/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.FaceFeature;
import com.yami.shop.bean.enums.FeatureTypeEnum;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.FaceFeatureService;
import org.springframework.beans.factory.annotation.Autowired;
import com.yami.shop.common.response.ServerResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 五官资源管理控制器
 * @author
 */
@RestController
@RequestMapping("/admin/faceFeature")
public class FaceFeatureController {

    @Autowired
    private FaceFeatureService faceFeatureService;

    /**
     * 分页获取五官资源列表
     */
    @GetMapping("/page")
//    @PreAuthorize("@pms.hasPermission('admin:faceFeature:page')")
    public ServerResponseEntity<IPage<FaceFeature>> page(FaceFeature faceFeature, PageParam<FaceFeature> page) {
        IPage<FaceFeature> faceFeaturePage = faceFeatureService.page(page, new LambdaQueryWrapper<FaceFeature>()
                .like(StrUtil.isNotBlank(faceFeature.getFeatureName()), FaceFeature::getFeatureName, faceFeature.getFeatureName())
                .eq(faceFeature.getFeatureType() != null, FaceFeature::getFeatureType, faceFeature.getFeatureType())
                .eq(faceFeature.getStatus() != null, FaceFeature::getStatus, faceFeature.getStatus()));
        return ServerResponseEntity.success(faceFeaturePage);
    }

    /**
     * 获取五官资源详情
     */
    @GetMapping("/info/{faceFeatureId}")
//    @PreAuthorize("@pms.hasPermission('admin:faceFeature:info')")
    public ServerResponseEntity<FaceFeature> info(@PathVariable("faceFeatureId") String faceFeatureId) {
        FaceFeature faceFeature = faceFeatureService.getById(faceFeatureId);
        return ServerResponseEntity.success(faceFeature);
    }

    /**
     * 新增五官资源
     */
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('admin:faceFeature:save')")
    public ServerResponseEntity<Void> save(@RequestBody FaceFeature faceFeature) {
        faceFeature.setCreateTime(new Date());
        faceFeature.setUpdateTime(new Date());
        faceFeatureService.save(faceFeature);
        return ServerResponseEntity.success();
    }

    /**
     * 修改五官资源
     */
    @PutMapping
//    @PreAuthorize("@pms.hasPermission('admin:faceFeature:update')")
    public ServerResponseEntity<Void> update(@RequestBody FaceFeature faceFeature) {
        faceFeature.setUpdateTime(new Date());
        faceFeatureService.updateById(faceFeature);
        return ServerResponseEntity.success();
    }

    /**
     * 删除五官资源
     */
    @DeleteMapping
//    @PreAuthorize("@pms.hasPermission('admin:faceFeature:delete')")
    public ServerResponseEntity<Void> delete(@RequestBody String[] faceFeatureIds) {
        faceFeatureService.removeByIds(Arrays.asList(faceFeatureIds));
        return ServerResponseEntity.success();
    }

    /**
     * 更新五官资源状态
     */
    @PutMapping("/status")
//    @PreAuthorize("@pms.hasPermission('admin:faceFeature:update')")
    public ServerResponseEntity<Void> updateStatus(@RequestBody FaceFeature faceFeature) {
        FaceFeature updateFaceFeature = new FaceFeature();
        updateFaceFeature.setFeatureId(faceFeature.getFeatureId());
        updateFaceFeature.setStatus(faceFeature.getStatus());
        updateFaceFeature.setUpdateTime(new Date());
        faceFeatureService.updateById(updateFaceFeature);
        return ServerResponseEntity.success();
    }

    /**
     * 批量更新五官资源状态
     */
    @PutMapping("/batchStatus")
//    @PreAuthorize("@pms.hasPermission('admin:faceFeature:update')")
    public ServerResponseEntity<Void> batchUpdateStatus(@RequestBody FeatureBatchStatusRequest request) {
        if (request.getFeatureIds() == null || request.getFeatureIds().isEmpty()) {
            return ServerResponseEntity.showFailMsg("五官资源ID列表不能为空");
        }
        if (request.getStatus() == null) {
            return ServerResponseEntity.showFailMsg("状态不能为空");
        }
        
        for (String featureId : request.getFeatureIds()) {
            FaceFeature updateFaceFeature = new FaceFeature();
            updateFaceFeature.setFeatureId(Long.valueOf(featureId));
            updateFaceFeature.setStatus(request.getStatus());
            updateFaceFeature.setUpdateTime(new Date());
            faceFeatureService.updateById(updateFaceFeature);
        }
        
        return ServerResponseEntity.success();
    }

    /**
     * 批量删除五官资源
     */
    @DeleteMapping("/batch")
//    @PreAuthorize("@pms.hasPermission('admin:faceFeature:delete')")
    public ServerResponseEntity<Void> batchDelete(@RequestBody FeatureBatchDeleteRequest request) {
        if (request.getFeatureIds() == null || request.getFeatureIds().isEmpty()) {
            return ServerResponseEntity.showFailMsg("五官资源ID列表不能为空");
        }
        
        faceFeatureService.removeByIds(request.getFeatureIds());
        return ServerResponseEntity.success();
    }
}

/**
 * 五官资源批量状态更新请求类
 */
class FeatureBatchStatusRequest {
    private List<String> featureIds;
    private Integer status;

    public List<String> getFeatureIds() {
        return featureIds;
    }

    public void setFeatureIds(List<String> featureIds) {
        this.featureIds = featureIds;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }
}

/**
 * 五官资源批量删除请求类
 */
class FeatureBatchDeleteRequest {
    private List<String> featureIds;

    public List<String> getFeatureIds() {
        return featureIds;
    }

    public void setFeatureIds(List<String> featureIds) {
        this.featureIds = featureIds;
    }
}